package cn.sunshine.system.service.impl;

import cn.sunshine.system.domain.Role;
import cn.sunshine.system.mapper.RoleMapper;
import cn.sunshine.system.service.IRoleService;
import cn.sunshine.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Jiang
 * @since 2022-07-12
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void save(Role role) {
        roleMapper.save(role);
        // 批量增加角色权限中间表
        roleMapper.saveRolePermessions(role.getId(), role.getPermissions());
        // 批量增加角色菜单中间表
        roleMapper.saveRoleMenus(role.getId(), role.getMenus());
    }

    /*
     * @Title: update
     * @Description: 重写的更新
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/7/13 15:28
     * @Parameters: [role]
     * @Return void
     */
    @Override
    public void update(Role role) {
        // 更新
        roleMapper.update(role);
        // 删除数据库中已经存在的权限
        roleMapper.removeRolePermessions(role.getId());
        // 删除数据库中已经存在的菜单
        roleMapper.removeRoleMenus(role.getId());
        // 然后再批量新增
        // 批量增加角色权限中间表
        roleMapper.saveRolePermessions(role.getId(), role.getPermissions());
        // 批量增加角色菜单中间表
        roleMapper.saveRoleMenus(role.getId(), role.getMenus());
    }

    /*
     * @Title: remove
     * @Description: 删除
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/7/13 15:58
     * @Parameters: [id]
     * @Return void
     */
    @Override
    public void remove(Long id) {
        // 删除数据库中已经存在的权限
        roleMapper.removeRolePermessions(id);
        // 删除数据库中已经存在的菜单
        roleMapper.removeRoleMenus(id);
        roleMapper.remove(id);
    }
}
